package com.yangyang.lamviarm.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.yangyang.lamviarm.util.TypeEnum;

public class JActionModelDef extends JBaseDef {

	public static HashMap<String, JActionModelDef> mapNameActionModel;

	public List<String> mPrecondition;
	public List<String> mEffectA;
	public List<String> mEffectD;
	public List<String> mActionVariable;
	public List<Integer> mQValueList;
	private int mQValue; // Qֵ
	private int mOldQValue;

	public void setQValue(int value) {
		this.mOldQValue = this.mQValue;
		this.mQValue = (int) (value * 0.6 + this.mOldQValue * 0.4);
		this.mQValueList.add(this.mQValue);
	}

	public int getQValue() {
		return this.mQValue;
	}

	public int getOldQValue() {
		return this.mOldQValue;
	}

	public JActionModelDef(String name, List<String> variable) {
		super(name);
		this.mActionVariable = variable;
		this.mPrecondition = new ArrayList<String>();
		this.mEffectA = new ArrayList<String>();
		this.mEffectD = new ArrayList<String>();
		this.mQValueList = new ArrayList<Integer>();
	}

	public JActionModelDef(String name, String[] variable) {
		super(name);
		this.mActionVariable = new ArrayList<String>();
		for (int i = 0; i < variable.length; i++)
			this.mActionVariable.add(variable[i]);
		this.mPrecondition = new ArrayList<String>();
		this.mEffectA = new ArrayList<String>();
		this.mEffectD = new ArrayList<String>();
	}

	@Override
	public JActionModelDef clone() {
		JActionModelDef result = new JActionModelDef(this.mName,
				this.mActionVariable);
		for (String str : mPrecondition) {
			result.mPrecondition.add(str);
		}
		for (String str : mEffectA) {
			result.mEffectA.add(str);
		}
		for (String str : mEffectD) {
			result.mEffectD.add(str);
		}
		return result;
	}

	public boolean bePrecondition(String pre) {
		for (String str : mPrecondition) {
			if (JPredicate.contraPredicate(str, pre))
				return false;
		}
		return !mPrecondition.contains(pre);
	}

	public boolean beEffectA(String pre) {
		return !mPrecondition.contains(pre) && !mEffectA.contains(pre);
	}

	public boolean bEffectD(String pre) {
		return mPrecondition.contains(pre) && !mEffectD.contains(pre);
	}

	public JActionModelDef(List<String> action, List<String> precondition,
			List<String> effecta, List<String> effectd) {
		// TODO Auto-generated constructor stub
		super(action.get(0));
		this.mPrecondition = precondition;
		this.mEffectA = effecta;
		this.mEffectD = effectd;
		this.mActionVariable = action.subList(1, action.size());

		if (mapNameActionModel == null)
			mapNameActionModel = new HashMap<String, JActionModelDef>();
		mapNameActionModel.put(this.mName, this);
	}

	@Override
	public String toString() {
		String msg = "   Name(" + this.mName + ")  ";
		msg += " precondition(";
		for (String str : this.mPrecondition)
			msg += str + " ";
		msg += ") effecta(";
		for (String str : this.mEffectA)
			msg += str + " ";
		msg += ") effectd(";
		for (String str : this.mEffectD)
			msg += str + " ";
		msg += ")   ";
		return msg;
	}

	public String toQListString() {
		String msg = "";
		msg += " valuelist(";
		for (int i = 0; i < this.mQValueList.size(); i++) {
			msg += " " + this.mQValueList.get(i);
		}
		msg += ")";
		return msg;
	}
}
